﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Voting Validator</title>
	<style type="text/css">
		@import url('global.css');
	</style>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<meta http-equiv="Access-Control-Allow-Origin" content="*" />
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
	<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
	<script type="text/javascript" src="voting-sites-crawler.js"></script>
	<script type="text/javascript" src="votes-downloader.js"></script>
	<script type="text/javascript">
		var campaignRootUrl = 'http://www.vybory.izbirkom.ru/region/izbirkom?action=show&root_a=1000032&vrn=100100031793505&region=0&global=true&type=0&sub_region=0&prver=0&pronetvd=null';
//			var campaignRootUrl = 'http://www.vybory.izbirkom.ru/region/izbirkom?action=show&global=1&vrn=100100028713299&region=0&prver=0&pronetvd=null';

			function runPlotting(votesHierarchycal, votesPlain) {
				$(document.body).text(JSON.stringify(votesHierarchycal));
			}

			$(document).ready(function () {
				if (window.votesPlain == undefined || confirm('Are you sure you want to reload votes data?')) {

					var crawler = new VotingSitesCrawler(campaignRootUrl);
					var $progress = $('span.progress');

					function makePlainSites(nodesTree, nodesPlain) {
						$.each(nodesTree, function (i, n) {
							if (n.subordinates == undefined)
								nodesPlain.push(n);
							else
								makePlainSites(n.subordinates, nodesPlain);
						});
					}

					crawler.run(function (hierarchy) {
						var votingSitesPlain = [];

						makePlainSites(hierarchy, votingSitesPlain);
						var downloader = new VotesDownloader(votingSitesPlain, function (amountDone, finished) {
							$('span', $progress).css('width', (100.0 * amountDone / votingSitesPlain.length) + '%');
							if (finished) {
								window.votesHierarchycal = hierarchy;
								window.votesPlain = votingSitesPlain;
								runPlotting(hierarchy, votingSitesPlain);
							}
						});

						downloader.run();
					});
				} else {
					runPlotting(window.votesHierarchycal, window.votesPlain);
				}
			});


	</script>
	</head>
	<body>
		<p>Results downloaded:<span class="progress"><span></span></span></p>
	</body>
</html>
